package pub.xspace.nova.article.controller;

import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.core.bean.BeanUtil;
import jakarta.annotation.Resource;
import pub.xspace.nova.article.domain.Article;
import pub.xspace.nova.article.service.ArticleService;
import pub.xspace.nova.common.controller.BaseController;
import pub.xspace.nova.common.domain.AjaxResult;

@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {

	@Resource
	private ArticleService articleService;

	@PostMapping("/getPage")
	public AjaxResult getPage(@RequestBody Map<String, Object> params) {
		Article article = BeanUtil.toBean(params.get("article"), Article.class);
		return success(articleService.getPage(Integer.valueOf(String.valueOf(params.get("pageNum"))),
				Integer.valueOf(String.valueOf(params.get("pageSize"))), article));
	}

	@PostMapping("/save")
	public AjaxResult save(@RequestBody Article article) {
		articleService.insert(article);

		return success();
	}

	@PutMapping("/update")
	public AjaxResult update(@RequestBody Article article) {

		articleService.updateById(article);
		return success(article);
	}

	@DeleteMapping("/deleteById")
	public AjaxResult deleteById(String id) {
		articleService.deleteById(id);
		return success();
	}

	@DeleteMapping("/deleteByIds")
	public AjaxResult deleteByIds(@RequestBody List<String> ids) {
		articleService.deleteByIds(ids);
		return success();
	}
}
